package studysystem.demo.mapper;

import org.apache.ibatis.annotations.*;
import studysystem.demo.bean.Manager;
import studysystem.demo.bean.Permission;
import studysystem.demo.bean.Role;

import java.util.List;

@Mapper
public interface ManagerMapper {

    //获取管理员角色
    @Select("select role from manager where jobnum = #{jobnum,jdbcType=VARCHAR}")
    Role findRole(String jobnum);

    //获取用户权限---UNDO
    @Select("select * from permission where role = #{role,jdbcType=VARCHAR}")
    List<Permission> findPermission(String role);


    //修改管理员密码
    @Update("update manager set password=#{password,jdbcType=VARCHAR} where jobnum=#{jobnum,jdbcType=VARCHAR}")
    void updatePsw(Manager adminEntity);

    //新建管理员
    @Insert("insert into manager (jobnum, name, phone, sex, userid, password, role) values (#{jobnum,jdbcType=VARCHAR}," +
            " #{name,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, #{userid,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, '管理员')")
    void saveAdmin(Manager adminEntity);

    //更新管理员信息
    @Update("update manager set name = #{name,jdbcType=VARCHAR}, phone = #{phone,jdbcType=VARCHAR}, sex = #{sex,jdbcType=VARCHAR},  userid = #{userid,jdbcType=VARCHAR} where jobnum = #{jobnum,jdbcType=VARCHAR}")
    void updateAdmin(Manager adminEntity);

    //删除管理员
    @Delete("delete from manager where jobnum = #{jobnum,jdbcType=VARCHAR}")
    void deleteAdmin(String jobnum);

    //查询管理员信息
    @Select("select jobnum, name, phone, sex, userid, password, role from manager where jobnum = #{jobnum,jdbcType=VARCHAR}")
    Manager findAdminInfo(String jobnum);

    //查询管理员密码
    @Select("select password from manager where jobnum = #{jobnum,jdbcType=VARCHAR}")
    Manager findAdminPsw(String jobnum);

    //查询所有管理员条数
    @Select("select count(*) from manager where role = '管理员'")
    int findAdminCountAll();

    //查询所有管理员
    @Select("select jobnum, name, phone, sex, userid, role from  manager where role = '管理员' order by name desc")
    List<Manager> findAdminAll();

    //查询管理员条数 (用户名  模糊查询)
    @Select("select count(*) from  manager where jobnum like CONCAT(CONCAT('%', #{jobnum}), '%') and role = '管理员'")
    int findAdminCountByJobnum(String jobnum);

    //查询管理员(用户名  模糊查询)
    @Select("select jobnum, name, phone, sex, userid, role from  manager where jobnum like CONCAT(CONCAT('%', #{jobnum}), '%') and role = '管理员' order by name desc")
    List<Manager> findAdminByJobnum(String jobnum);

    //查询管理员条数 (姓名  模糊查询)
    @Select("select count(*) from  manager where name like CONCAT(CONCAT('%', #{name}), '%') and role = '管理员'")
    int findAdminCountByName(String name);

    //查询管理员(姓名  模糊查询)
    @Select("select jobnum, name, phone, sex, userid, role from  manager where name like CONCAT(CONCAT('%', #{name}), '%') and role = '管理员' order by name desc")
    List<Manager> findAdminByName(String name);

}
